package org.rc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.rc.dto.RoomPageDto;
import org.rc.entity.Room;
import org.rc.mapper.RoomMapper;
import org.rc.service.RoomService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    @Override
    public Long add(String name) {
        Room room = Room.getInstance(name);
        this.save(room);
        return room.getId();
    }

    @Override
    public void updateRoom(Room room) {
        this.updateById(room);
    }

    @Override
    public void deleteRoom(Long roomId) {
        this.removeById(roomId);
    }

    @Override
    public List<Room> getByRoomIds(List<Long> roomIds) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",roomIds);
        return this.list(queryWrapper);
    }

    @Override
    public void deleteRooms(List<Long> roomIds) {
        this.removeByIds(roomIds);
    }

    @Override
    public Page<Room> getRoomPage(RoomPageDto pageDto) {
        Page<Room> page = new Page<>(pageDto.getPageNo(),pageDto.getPageSize());
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(pageDto.getName()),"name",pageDto.getName());
        return this.baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Room getByRoomId(Long roomId) {
        return this.getById(roomId);
    }

    @Override
    public List<Room> getByRoomName(String name) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return list(queryWrapper);
    }

    @Override
    public List<Room> getByRoomName(String name, Long id) {
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name).ne("id",id);
        return list(queryWrapper);
    }

    @Override
    public List<Room> getAllRooms() {
        return list();
    }
}
